package com.tianqu.tms.www.api.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.enums.GateResult;
import com.tianqu.tms.core.webservice.TmsCarGateService;
import com.tianqu.tms.www.api.form.InputParam;
import com.tianqu.tms.www.api.form.OutputParam;
import com.tianqu.tms.www.api.form.ResultModel;
import com.tianqu.tms.www.api.network.PSHTTPUtil;

/**
 * @author 
 *
 */
public class TmsCarGateAction extends TmsAjaxAction {
	private Logger LOGGER = Logger.getLogger(TmsCarGateAction.class);
	private TmsCarGateService carGateService = new TmsCarGateService();
	
	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, UserContext uc) throws TmsException {
		String json = request.getParameter("param");
		String result = "";
		ResultModel<OutputParam> rop = new ResultModel<OutputParam>(); 
		OutputParam responseObj = new OutputParam();
		try {
			InputParam requestObj = PSHTTPUtil.convertJsonToJavaBean(json, InputParam.class);
			if(StringUtil.isNullOrBlank(requestObj.getCameraIp()) || StringUtil.isNullOrBlank(requestObj.getPicPath()) || requestObj.getDeviceId() == 0){
				responseObj.setFirstByte(GateResult.RESULT_06.getValue());
			}else{
				responseObj.setFirstByte(carGateService.getPermission(requestObj));
			}
			rop.setResultCode(GateResult.SUCCESS.getValue());
			rop.setResultData(responseObj);
			result = PSHTTPUtil.convertJavaBeanToJson(rop);
		} catch (Exception e) {
			LOGGER.error("tmsCarGateAction web api error",e);
			result = GateResult.RESULT_06.getValue();
			rop.setResultCode(GateResult.RESULT_06.getValue());
		}
		LOGGER.debug("call car gateway:param:"+json +"   result:"+result);
		responseValues(response, result, TEXT);
	}
 
}
